<template>
  <div class="page-container-padding">
    <el-tabs v-model="curTabName">
      <el-tab-pane name="manage" label="报告管理">
        <div style="display: flex; align-items: center">
          <el-select v-model="manageData.curType" size="mini">
            <el-option
              v-for="t in reportType"
              :key="t.key"
              :value="t.key"
              :label="t.name"
            />
          </el-select>
          <el-button
            size="mini"
            type="primary"
            style="margin-left: 6px"
            :loading="manageData.queryLoading"
            @click="toQueryManageData"
          >
            查询
          </el-button>
        </div>
        <div style="margin-top: 16px">
          <el-table border stripe :data="manageData.dataList">
            <el-table-column prop="id" label="ID" width="80" />
            <el-table-column prop="file_date" label="日期" />
            <el-table-column prop="variety_en" label="品种" />
            <el-table-column prop="title" label="标题" />
            <el-table-column prop="file_type" label="类型" />
            <el-table-column prop="reading" label="阅读量" width="80" />
            <el-table-column label="状态" width="110" align="center">
              <template #default="scope">
                <el-tag
                  size="mini"
                  :type="scope.row.is_active ? 'success' : 'danger'"
                >
                  {{ scope.row.is_active ? '公开' : '隐蔽' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="60" align="center">
              <template #default="scope">
                <el-button type="text" @click="toUpdateReport(scope.row)">
                  编辑
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div style="margin-top: 8px">
          <el-pagination
            background
            hide-on-single-page
            layout="total, prev, pager, next, jumper"
            :current-page="manageData.page"
            :page-size="manageData.pageSize"
            :total="manageData.totalCount"
            @current-change="currentPageChanged"
          />
        </div>
      </el-tab-pane>
      <el-tab-pane name="create" label="新增">
        <div style="display: flex; align-items: center">
          <el-date-picker v-model="newData.queryDate" size="mini" />
          <el-button
            type="primary"
            size="mini"
            style="margin-left: 8px"
            :loading="newData.queryLoading"
            @click="getDateReportList"
          >
            获取
          </el-button>
          <el-button
            type="success"
            size="mini"
            style="margin-left: 8px"
            :loading="newData.saveLoading"
            @click="saveDateReportList"
          >
            保存
          </el-button>
        </div>
        <div style="margin-top: 16px">数量：{{ newData.dataList.length }}</div>
        <div style="margin-top: 16px">
          <el-table border stripe :data="newData.dataList">
            <el-table-column prop="id" label="ID" width="70" />
            <el-table-column prop="file_date" label="日期" width="130" />
            <el-table-column prop="file_type" label="类型" width="110" />
            <el-table-column prop="title" label="名称" />
            <el-table-column prop="variety_en" label="品种" width="120" />
            <el-table-column label="操作" width="70">
              <template #default="scope">
                <el-button
                  type="text"
                  size="mini"
                  @click="toAddReport(scope.row)"
                >
                  添加
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane>
    </el-tabs>
    <el-dialog :visible.sync="curRow.isShow" :title="curRow.title">
      <el-form size="mini" label-width="80px">
        <el-form-item label="标题">
          <div>{{ curRow.title }}</div>
        </el-form-item>
        <el-form-item label="类型">
          <div>{{ curRow.file_type }}</div>
        </el-form-item>
        <el-form-item label="位置">
          <div>{{ curRow.filepath }}</div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="curRow.is_active"
            active-text="公开"
            inactive-text="隐蔽"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="curRow.isShow = false">取消</el-button>
        <el-button
          type="primary"
          :loading="curRow.saveLoading"
          @click="toSaveUpdateRow"
        >
          确定保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { datetimeToStr, parseIntegerTime } from '@/utils/datetimeUtils'
  import {
    RetrievePeriodReport,
    CreatePeriodReport,
    UpdatePeriodReport,
  } from '@/api/adminRuizhi/dailyMaintain'
  import { GetDatePeriodReport } from '@/api/departmentWork/periodReport'
  export default {
    data() {
      return {
        curTabName: 'manage',
        reportType: [
          {
            key: 'EARLY',
            name: '晨会纪要',
          },
          {
            key: 'DAILY',
            name: '收盘日评',
          },
          {
            key: 'WEEKLY',
            name: '每周一报',
          },
          {
            key: 'MONTHLY',
            name: '每月研报',
          },
          {
            key: 'ANNUAL',
            name: '年度报告',
          },
        ],
        manageData: {
          curType: 'EARLY',
          queryLoading: false,
          dataList: [],
          page: 1,
          pageSize: 30,
          totalCount: 0,
        },
        curRow: {
          isShow: false,
          rid: -1,
          file_date: '',
          title: '',
          filepath: '',
          is_active: '',
          saveLoading: false,
        },

        newData: {
          queryLoading: false,
          queryDate: new Date(),
          dataList: [],
        },
      }
    },
    mounted() {},
    methods: {
      toQueryManageData() {
        this.manageData.page = 1
        this.getManageData()
      },
      currentPageChanged(page) {
        this.manageData.page = page
        this.getManageData()
      },

      getManageData() {
        RetrievePeriodReport(
          this.manageData.curType,
          this.manageData.page,
          this.manageData.pageSize
        ).then((res) => {
          this.manageData.dataList = res.data.rows
          this.manageData.totalCount = res.data.total_count
        })
      },

      // 获取新数据
      getDateReportList() {
        const qDate = datetimeToStr(this.newData.queryDate, true, '')
        this.newData.queryLoading = true
        GetDatePeriodReport(qDate).then((res) => {
          this.newData.dataList = res.data.map((item) => {
            return {
              id: item.id,
              file_date: datetimeToStr(
                parseIntegerTime(item.file_date),
                true,
                '-'
              ),
              file_type: item.file_type,
              title: item.title,
              variety_en: item.variety_en,
              filepath: item.filepath,
            }
          })
          this.newData.queryLoading = false
        })
      },

      // 保存新数据
      saveDateReportList() {
        if (this.newData.dataList.length < 1) {
          this.$baseMessage('请先查询数据再保存!', 'error')
          return
        }
        this.newData.saveLoading = true
        CreatePeriodReport(this.newData.dataList).then((res) => {
          this.$baseMessage(res.message, 'success')
          this.newData.dataList = []
          this.newData.saveLoading = false
        })
      },

      // 保存一条新数据
      toAddReport(row) {
        CreatePeriodReport([row]).then((res) => {
          this.$baseMessage(res.message, 'success')
        })
      },

      // 准备修改当前报告行
      toUpdateReport(row) {
        this.curRow.rid = row.id
        this.curRow.file_date = row.file_date
        this.curRow.title = row.title
        this.curRow.file_type = row.file_type
        this.curRow.is_active = row.is_active
        this.curRow.filepath = row.filepath
        this.curRow.isShow = true
      },
      toSaveUpdateRow() {
        const bodyData = {
          rid: this.curRow.rid,
          file_date: this.curRow.file_date,
          is_active: this.curRow.is_active,
        }
        this.curRow.saveLoading = true
        UpdatePeriodReport([bodyData]).then((res) => {
          this.$baseMessage(res.message, 'success')
          this.curRow.saveLoading = false
          this.curRow.rid = -1
          this.curRow.isShow = false
          this.getManageData()
        })
      },
    },
  }
</script>
